﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HerGoS.ControlsUI;
using HerGoS.Viaticos.Engine;
using HerGoS.WebControlExtend;

namespace HerGoS.WebUI.Viaticos.TipoConcepto
{
    public partial class TipoConceptos_Listado : PageBase
    {
        #region Propiedades
        public List<HerGoS.Viaticos.Entities.TipoConcepto> TiposConceptos
        {
            get
            {
                if (ViewState["TiposConceptos"] != null)
                    return (List<HerGoS.Viaticos.Entities.TipoConcepto>)ViewState["TiposConceptos"];
                return null;
            }
            set
            {
                ViewState["TiposConceptos"] = value;
            }

        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Initialize();
                LoadGridTipoConcepto(false);
            }
        }

        void Initialize()
        {
            this.GridTipoConcepto.Settings(new GridViewExtBasic());
            this.btnNuevo.Settings(new ButtonExtText());
            this.btnRefresh.Settings(new ButtonExtText());
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadGridTipoConcepto(false);
        }

        protected void btnBorrar_Click(object sender, EventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int index = Convert.ToInt32(btn.CommandArgument);
            int id = Convert.ToInt32(GridTipoConcepto.DataKeys[index].Value);


            HerGoS.Viaticos.Entities.TipoConcepto tipoConcepto = TiposConceptos.Find(c => c.OID == id);
            TiposConceptos.Remove(tipoConcepto);

            GestorTipoConcepto.Eliminar(tipoConcepto);
            LoadGridTipoConcepto(true);

        }

        //protected void btnFiltrar_Click(object sender, EventArgs e)
        //{
        //    var result = TiposConceptos.FindAll(y => y.Descripcion.ToLower().Contains(this.txtDescripcionBuscar.Text.ToLower()));

        //    this.GridTipoConcepto.DataSource = result;
        //    this.GridTipoConcepto.DataBind();
        //}

        //protected void btnQuitarFiltros_Click(object sender, EventArgs e)
        //{
        //    LoadGridTipoConcepto(true);

        //}







        private void LoadGridTipoConcepto(bool cache)
        {
            if (!cache)
                TiposConceptos = GestorTipoConcepto.Consultar();

            this.GridTipoConcepto.DataSource = TiposConceptos;
            this.GridTipoConcepto.DataBind();
        }

        protected void GridTipoConcepto_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButtonExt Editar = e.Row.FindControl("btnEditar") as ImageButtonExt;
                if (Editar != null)
                {
                    Editar.Settings(new ImageButtonExtEdit());
                    int oid = DataBinder.Eval(e.Row.DataItem, "OID").ToInteger();
                    Editar.OnClientClick = "Editar(" + oid.ToString() + "); return false;";
                }

                ImageButtonExt Borrar = e.Row.FindControl("btnBorrar") as ImageButtonExt;
                if (Borrar != null)
                {
                    Borrar.Settings(new ImageButtonExtDelete());
                }

            }
        }

        protected void GridTipoConcepto_PageIndexChanging(Object sender, GridViewPageEventArgs e)
        {
            GridTipoConcepto.PageIndex = e.NewPageIndex;
            LoadGridTipoConcepto(true);
        }

      

    }
}